package cn.sibu.plugin.authorize.activity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.sibu.plugin.authorize.R;
import cn.sibu.plugin.authorize.bean.ExistsMobileEvent;
import cn.sibu.plugin.authorize.http.SibuAuthorizeHttp;
import cn.sibu.plugin.authorize.util.ButtonAvoidForceUtils;
import cn.sibu.plugin.authorize.util.CommonUtil;
import cn.sibu.plugin.authorize.util.DialogUtil;
import cn.sibu.plugin.authorize.util.ErrorContent;
import cn.sibu.plugin.authorize.util.RegexRule;
import cn.sibu.plugin.authorize.util.RequestParamName;
import cn.sibu.plugin.authorize.util.ResponseCode;
import cn.sibu.plugin.authorize.view.EditTextWithDelete;
import core.dl.internal.DLIntent;
import de.greenrobot.event.EventBus;

/**
 * Created by 刘红亮 on 2015/5/29 10:31
 */
public class ForgetPasswordActivity extends BaseActivity implements OnClickListener {

    @InjectView(R.id.bt_forgetpwd_confirm)
    Button bt_forgetpwd_confirm;

    @InjectView(R.id.btn_backlogin)
    Button btn_backlogin;

    @InjectView(R.id.et_forgetpwd_phone)
    EditTextWithDelete et_forgetpwd_phone;

    private String mobile;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_forget_password);
        EventBus.getDefault().register(this);
        ButterKnife.inject(this);
        initUI();
    }

    private void initUI() {
        bt_forgetpwd_confirm.setOnClickListener(this);
        btn_backlogin.setOnClickListener(this);
        //软件键显示数字
        et_forgetpwd_phone.setInputType(InputType.TYPE_CLASS_NUMBER);
    }

    @Override
    public void onClick(View v) {
        if (ButtonAvoidForceUtils.filter()) {
            switch (v.getId()) {
                case R.id.bt_forgetpwd_confirm:
                    getBindPhoneNum();
                    break;

                case R.id.btn_backlogin:
                    backLoginUI();
                    break;
            }
        }
    }


    /**
     *
     * TODO 得到手机号，进行联网请求
     *
     * @param
     * @throw
     * @return void
     */
    public void getBindPhoneNum() {


        mobile = et_forgetpwd_phone.getText().toString().trim();
        if (TextUtils.isEmpty(mobile)) {
            Toast.makeText(that, ErrorContent.PHONENULL,
                    Toast.LENGTH_SHORT).show();
            return;
        }

        // 进行手机号的正则匹配，以此减少访问服务器的次数
        if(!CommonUtil.regexMate(RegexRule.PHONENUM, mobile)){
            Toast.makeText(that, ErrorContent.PHONEILLEGAL,
                    Toast.LENGTH_SHORT).show();
            return;
        }
		

		/*RequestParams params = new RequestParams();
		params.addBodyParameter("mobile", mobile);
		params.addBodyParameter("at", HMApi.FORPWD);*/
        //缓冲效果
        DialogUtil.showDialogForLoading(that, "正在加载...", true);
        SibuAuthorizeHttp.existsMobile(mobile, RequestParamName.FORPWD_TYPE, that);

    }
    public void onEventMainThread(ExistsMobileEvent existsMobileEvent){
        DialogUtil.dismissDialogForLoading();
        if (ResponseCode.SUCCEED.equals(existsMobileEvent.code)) {
            DLIntent intent = new DLIntent(getPackageName(),
                    AuthcodeActivity.class);
            intent.putExtra("mobile", mobile);
            intent.putExtra(AuthcodeActivity.CODETYPE,RequestParamName.FORGETPWD_AUTHCODE);
            startPluginActivity(intent);
        } else{
            if(existsMobileEvent.msg==null){
                Toast.makeText(that, "连接失败",
                        Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(that, existsMobileEvent.msg,
                        Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}
